﻿ 
(function ($) {
	$.fn.selectDialog  = function(operation,data){
		var id = $(this).attr('id');
		var $dialog =$('#'+id);
		var setting = $dialog.data('selectDialog');
		if(operation=="filter"){
			filter(this);
			return;
		}
		if(operation=="put"){
			putSelectData(id,data);
			return;
		}
		if(operation=="select"){
			selectRow(data,this);
			return;
		}
		if(operation=="show"){
			$dialog.show();
			$($dialog.find('input')[0]).focus();
			$dialog.find('input').each(function(){
				$(this).val('');
			});
			return;
		}
		if(operation=="button"){
			button(data);
			return;
		}
		 
		if(operation=="init"){
			var mapper = data.mapper;
			var headerSize = data.headerSize;
			var reload = data.reload;
			var button = data.button;
			var hasInput = data.hasInput;
			var dialogStyle = data.dialogStyle;
			if(dialogStyle==null){
				dialogStyle="";
			}
			if(hasInput!=null&&hasInput==true){
				hasInput = true;
            }else{
            	hasInput = false;
            }
			var width = "300px"; 
			var height = "300px";
			if(data.width!=null){
				width = data.width+"px";
			}
			if(data.height!=null){
				height = data.height+"px";
			}
			//加载css
			$("<link>").attr({ rel: "stylesheet",type: "text/css",href: _webApp+"/css/backstage-frame/pop/pop.css"}).appendTo("head");
			$('<style> .selectRow {background: #88BAF1;}</style>').appendTo("head");
			if(!$(this).hasClass('pop-box') || reload){
				$(this).addClass('pop-box');
				var html = '<div class="checking-dialog" style="'+dialogStyle+'" >'
					html += '<div class="dialog-withicon" style="width:'+width+'"><div class="pop-title" style="height: 30px;"> <span class="pop-title-name" style="margin-left: -1px;">选择</span></div>' ;
		            html += '<div class="with-body"><div class="with-table-wrap"><div style="overflow: scroll;height:'+height+';"><table><tbody><tr data-type="head">';
		            for(key in mapper){
		            	var h = "";
		            	if(headerSize!=undefined && headerSize.length>0){
		            		for(var i = 0 ;i<headerSize.length;i++){
		            			if(headerSize[i].key == key){
		            				h = '<th style="width:'+headerSize[i].width+'">';
		            			}
		            		}
	            		}
		            	if(h==""){
		            		h += '<th>';
		            	} 
		            	html += h + '<input type="text" autocomplete="off" style="width: 98%;height: 140%;" data-role="none" id="'+key+'" onkeyup=$("#'+id+'").selectDialog("filter") placeholder = "'+mapper[key]+'"/></th>';
		            	i++;
		            };
		            if(hasInput){
		            	html += "<th></th>";
		            }
	                html += '</tr></tbody></table></div></div>';
	                html += '<div class="bui-stdmod-footer" style="text-align: center;margin-top: 10px;">';
	                if(button!=null){
	                	for(var i = 0;i<button.length;i++){
	                		var btn = button[i];
	                		html += '<button class="button-primary" onclick=$("#'+id+'").selectDialog("button",this) >'+btn.name+'</button>';
	                		
	                	}
	                	
	                }
	                html += '</div></div>';
		            html += '<div class="gray-deletebt" style="font-size: 34px;width: 34px;height: 34px;margin-top: 5px;" onclick=$("#'+id+'").hide()>x</div></div></div>';
		            $(this).empty().append(html);
		            //请求数据
		            $.ajax({
		                type: "GET",
		                url: data.url,
		                dataType: "json",
		                success: function(res){
		            	    if(res.code!=0){
		            	    		$('#'+id +' .pop-title').empty().append('<span style="margin-left:-3px">'+res.msg+"</span>");
		            	    }
		            		for(var i = 0;i<res.data.length;i++){
		            			res.data[i].rownumber = i;
		            		} 
		            		$('#'+id).data ('selectDialog',{
    							data:res.data,
    							mapper:mapper,
    							id:id,
    							select:data.select,
    							formatter:data.formatter,
    							setting:data});
		            		 putSelectData(id,res.data ,hasInput);
		            		 if(data.useDefault!=null && data.useDefault){
		            			 $('#'+id).find('.isDefault').click();
		            		 }
		            		 
		            }});
			}
			return;
		}
		 function selectRow(rownumber,dialog){
			 setting.select(setting.data[rownumber]); 
			 $(dialog).hide();  
		 }
		function putSelectData(id,rows){
			 var html ="";
			 var setting = $('#'+id).data('selectDialog');
			 for(i=0;i<rows.length;i++){
 			   row=rows[i];
 			   var isDefault = " ";
 			   if(row.isDefault!=null && row.isDefault == 1){
 				  isDefault += "isDefault ";
 			   }
 			  html += '<tr class=" '+isDefault+' " onmouseover="focusTR(this)"   data-rownumber="'+row.rownumber+'"   ';
 			  if(!setting.setting.hasInput){
 				  html += 'onclick=$("#'+id+'").selectDialog("select","'+row.rownumber;
 			   } 
 			   html  += '")>';
 			   if( setting.formatter!=null){
 				   html += setting.formatter(row);
 			   }else{
 				  for( key in setting.mapper){
 	 				   html+=' <td>'+row[key]+'</td>';
 	 			   } 
 			   } 
 			  
 			   if(setting.setting.hasInput){
 				  html+=' <td><input type="text" name="input" style="width: 90%;height: 90%;"/></td>';
 			   } 
 			   html += '</tr>';
 		   	 }
			 
			 $('#'+setting.id+' tbody tr').each(function(){
				 if($(this).data('type')!='head'){
					 $(this).remove();
				 }
			 });
 		   	$('#'+setting.id+' tbody').append(html);
		 }
		  
		 function filter(dom){
			 var setting = $(dom).data('selectDialog');
			 if(event.keyCode == 40 || event.keyCode == 38){
				 var isDown = event.keyCode == 40;
				 var $selectRow = $('#'+setting.id+' tbody .selectRow');
				 if( $selectRow.length==0){
					 $selectRow = $($('#'+setting.id+' tbody tr')[1]);
				 }else{
					 $selectRow.removeClass('selectRow');
					 if(isDown){
						 $selectRow = $selectRow.next();
					 }else{
						 $selectRow = $selectRow.prev();
					 }
				 }
				 $selectRow.addClass('selectRow');
				 return;
			 }
			 if(event.keyCode == 13){
				 var $selectRow = $('#'+setting.id+' tbody .selectRow');
				 if( $selectRow.length!=0){
					 $selectRow.click();
				 }
			 }
			  
			 var allConsumer = setting.data;
			 var dialogId = setting.id;
			 var inputData = {};
			 for( key in setting.mapper){
				 inputData[key]  = $("#"+dialogId+' #'+key).val();
			 } 
			 var filterData = [];
			 
			 for(var i =0;i<allConsumer.length;i++){
				 var row = allConsumer[i];
				 var isAllEmpty = true;
				 for(var key in inputData){
					 if(inputData[key]!=""){
						 isAllEmpty = false;
					 }
				 } 
				 if(isAllEmpty){
					 filterData.push(row);
					 continue;
				 }
				 var find = true;
				 for(var key in inputData){
					 var value = inputData[key];
					 if(row[key]!=null){
						 if(value!='' && row[key].toUpperCase().indexOf(value.toUpperCase())==-1){
							 find = false;
						 }
					 }
				 }
				 if(find){
					 filterData.push(row);
				 }
				  
			 }
			 $(dom).selectDialog("put",filterData);
		 }
		 function button(dom){
			 var rows = [];
			 $dialog.find('tr').each(function(){
				 var rownumber = $(this).data('rownumber');
				  
				 if(rownumber >=0){
					 var row = setting.data[rownumber];
					 var $input = $(this).find('input');
					 row[$input.attr('name')] = $input.val();
					 rows.push(row);
				 }
			 });
			 for(var i = 0 ;i<setting.setting.button.length;i++){
				 var btn = setting.setting.button[i];
				 if(btn.name ==$(dom).html()){
					 btn.click(rows);
				 }
			 }
		 }
	};
})(jQuery); 
function focusTR(dom){
	$(dom).addClass('selectRow').siblings().removeClass('selectRow');
}